package com.cf.crud.service;

/**
 * cf-game-BaseService
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/3/1 18:14
 */


import cn.hutool.core.map.MapUtil;
import com.cf.crud.entity.BaseEntity;
import com.cf.crud.mapper.BaseMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * wx-hongbao-BaseService
 *
 * @author <a href="2428922347@qq.com">Zhu mingming</a>
 * @info
 * @since 2018/1/15 15:02
 */
public abstract class AbsBaseService<T extends BaseEntity, M extends BaseMapper<T>> implements BaseService<T> {

    @Autowired
    protected M mapper;

    public T insert(T bean) {
        int insert = mapper.insert(bean);
        return insert > 0 ? bean : null;
    }

    public boolean inserts(List<T> beans) {
        int insert = mapper.insertListWithId(beans);
        return insert > 0 ? true : false;
    }

    public int delete(T bean) {
        return mapper.delete(bean);
    }

    public Integer deleteById(long id) {
        return this.mapper.deleteByPrimaryKey(id);
    }

    public int update(T bean) {
        return mapper.updateByPrimaryKey(bean);
    }


    public List<T> selectByIds(List<Long> ids) {
        HashMap<String, List<Long>> id = MapUtil.of("id", ids);
        return commonSearch(id, 1, Integer.MAX_VALUE);
    }

    public T selectOne(long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<T> select(T bean) {
        return mapper.select(bean);
    }

    public Page<T> selectByPage(Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page) mapper.selectAll();
    }

    public Page<T> selectByPage(T bean, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page) mapper.select(bean);
    }

    public List<T> selectAll() {
        return this.mapper.selectAll();
    }

    @Override
    public Page<T> selectByExample(Object example, Integer page, Integer size) {
        PageHelper.startPage(page, size);
        return (Page<T>) this.mapper.selectByExample(example);
    }

    @Override
    public Page<T> commonSearch(Map parameters, Integer page, Integer size) {
        Example searchCriteria = createSearchCriteria(parameters);
        return selectByExample(searchCriteria, page, size);
    }

    protected Example createSearchCriteria(Map<String, Object> parameters) {
        Example example = new Example(getTargetClass());
        Example.Criteria criteria = example.createCriteria();

        if (parameters == null || parameters.size() <= 0)
            return example;

        for (Map.Entry<String, Object> parameter : parameters.entrySet()) {
            String key = parameter.getKey();
            Object value = parameter.getValue();
            if (key != null && value != null)
                if (value instanceof Collection) {
                    //处理集合条件
                    if (StringUtils.isNotEmpty(parameter.getKey()) && parameter.getValue() != null && ((Collection) parameter.getValue()).size() > 0) {
                        criteria.andIn(parameter.getKey(), (Collection) parameter.getValue());
                    }
                } else {
                    criteria.andEqualTo(key, value);
                }
        }
        return example;
    }

    @Override
    public List<T> selectByExample(Object example) {
        return selectByExample(example, 0, Integer.MAX_VALUE);
    }

}
